<template>
	<view class="content">
		<view class="uni-panel-c">
			<view class="uni-panel-top">
				<text class="uni-text-logo">复习历史</text>
			</view>
			<view class="uni-panel-bottom">
				<view class="uni-panel-item">
					<image class="uni-image" src="/static/img/back.png" mode="widthFix" @click="clickBack"></image>
					<text class="uni-text-item">学习日期</text>
					<text class="uni-text-item">复习单词数</text>
					<text class="uni-text-item">正确率</text>
				</view>
				<view class="uni-panel">
					<view class="uni-panel-book" v-for="(item, index) in listWord" :key="index" v-if="!isEveryday||!item.isTodayStudy">
						<view class="uni-word-image"></view>
						<text class="uni-text-word">{{item.dateStr}}</text>
						<text class="uni-text-word">{{item.wordsCount}}</text>
						<text class="uni-text-word">{{item.percent}}%</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { getStudyLogList, formatDate } from '@/api/api.js';
	import store from "../../store";
	import { HTTP_REQUEST_URL } from '@/config/app.js';
	export default {
		mounted() {
		        // #ifdef APP-PLUS
		        plus.screen.lockOrientation('landscape');
		        // #endif
		    },
		data() {
			return {
				userId: 0,
				accountName: '',
				user_type: 0,
				listWord: [],
			}
		},
		onLoad(options) {
			const userInfo = store.state.app.userInfo;
			if( userInfo){
				this.accountName = userInfo.nickname;
				this.userId = userInfo.id;
				this.user_type = userInfo.user_type;
			}
			let student = uni.getStorageSync('student_info');
			if( student){
				this.accountName = student.nickname;
				this.userId = student.id;
			}

			let that = this;
			let params = {pageSize:99, 
							params:{user_id: this.userId}};
			getStudyLogList(params).then(res => {
				if( 0 == res.code && res.data){
					
					for( let item of res.data.studyWords){
						item.wordsCount = item.words.split(',').length;
					}
					console.log(res.data);
					let ret = [];
					let inserts = [];
					for( let row of res.data.rows){	
						let findItem = res.data.studyWords.find(i=>i.id==row.study_id);
						if( !findItem){
							continue;
						}
						let dateStr = formatDate(new Date(row.gmt_create));
						if( inserts.findIndex(i=>i.dateStr==dateStr&&i.study_id==row.study_id) >= 0){
							// 当天记录已存在
							continue;
						}
						inserts.push({dateStr, study_id: row.study_id});
						
						let wordsCount = findItem.wordsCount;
						if( wordsCount <= 0){
							wordsCount = 1;
						}
						let noCount = row.no_count;
						if( noCount > wordsCount){
							noCount = wordsCount;
						}
						
						let retItem = ret.find(i=>i.dateStr==dateStr);
						if( !retItem){
							retItem = {dateStr, wordsCount:0, noCount:0};
							ret.push(retItem);
						}
						retItem.wordsCount += wordsCount;
						retItem.noCount += noCount;
						retItem.percent = parseInt((wordsCount-noCount)*100/wordsCount);
					}
					this.listWord = ret;
				}
			}).catch(error => {
				// 处理异常
				console.error('发生错误:', error);
			});
		},
		methods: {
			clickBack(){
				uni.navigateBack();
			},
		}
	}
</script>

<style>
	page {
		height: 100%;
	}
	.content {
		width: 100%;
		height: 100%;
		background-image: url('../../static/img/bg.jpg');
		background-size: 100% 100%;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.uni-panel-c {
		width: 90%;
		height: 100%;
		display: flex;
		flex-direction: column;
	}
	
	.uni-panel-top {
		width: 100%;
		height: 20%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.uni-image {
		width: 6%;
	}
	.uni-text-logo{
		color: white;
		font-size: 5vmin;
		text-align: center;
		font-weight: bold;
		letter-spacing: 2rpx;
	}
	.uni-text-count{
		color: white;
		font-size: 4vmin;
		text-align: center;
	}
	
	.uni-panel-bottom {
		width: 100%;
		height: 80%;
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	.uni-panel-item {
		background-color: #f5f7e2;
		width: 100%;
		height: 10%;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}
	.uni-text-item{
		font-size: 4vmin;
		color: royalblue;
		letter-spacing: 2rpx;
		margin: 0 2%;
		font-weight: bold;
	}
	
	.uni-panel {
		width:100%;
		height: 90%;
		display: flex;
		flex-direction: column;
		overflow: auto;
	}
	
	.uni-panel-book {
		width: 100%;
		margin-top: 1vmin;
		background-color: white;
		border-radius: 10rpx;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}
	
	.uni-word-image {
		height: 10vmin;
	}
	
	.uni-text-word{
		font-size: 4vmin;
		color: black;
		font-weight: bold;
		letter-spacing: 1rpx;
		margin: 0 2%;
	}
	
</style>
